package com.buzz.activity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;

import com.buzz.service.BackgroundService;
import com.buzz.utils.GlobalConst;

import java.util.Locale;

/**
 * Created by NickChung on 6/8/15.
 */
public class NavActivity extends Activity {
    MyApplication myApp;
    int delayCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);

        StrictMode.setThreadPolicy(
                new StrictMode
                        .ThreadPolicy
                        .Builder()
                        .detectDiskReads()
                        .detectDiskWrites()
                        .detectNetwork()
                        .penaltyLog()
                        .build());

        StrictMode.setVmPolicy(
                new StrictMode
                        .VmPolicy
                        .Builder()
                        .detectLeakedSqlLiteObjects()
                        .detectLeakedClosableObjects()
                        .penaltyLog()
                        .penaltyDeath()
                        .build());

        myApp = (MyApplication) getApplication();

        //检查WIFI和蓝牙
        myApp.checkWifi();
        myApp.checkBluetooth();

        //如果已经登入过则记下登入状态
        if (myApp.fileHelper.isFileExist(GlobalConst.PATH_SAVE + GlobalConst.FILENAME_USER_INFO)) {
            myApp.logonUser.isLogon = true;
            //获取显示语言及播放语言
            myApp.initUserInfo();
            myApp.setDisplayLang(myApp.logonUser.defaultLang, false);
            myApp.setVoiceLang(myApp.logonUser.voiceLang);
        } else {
            //设置默认显示语言
            //如果地区是HK则手动转换成hk,地区是TW则手动转换成tw
            if (Locale.getDefault().getCountry().toLowerCase().equals("hk")) {
                myApp.setDisplayLang("hk", true);
            } else if (Locale.getDefault().getCountry().toLowerCase().equals("tw")) {
                myApp.setDisplayLang("tw", true);
            } else {
                myApp.setDisplayLang(Locale.getDefault().getLanguage(), true);
            }
        }

        //如果有WIFI直接连接服务器
        if (myApp.wifiReady) {
            myApp.connectServer();
        } else {
            //如果没有WIFI则打开WIFI
            myApp.openWifi();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //10秒后连接服务器
                    myApp.connectServer();
                }
            }, 10 * 1000);

            delayCount += 10;

        }

        //如果没有蓝牙
        if (!myApp.btReady) {
            //打开蓝牙
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, 0);

            //5秒后再进入
            delayCount += 5;
        } else {
            //3秒后再进入
            delayCount += 3;
        }

        accessRoute(delayCount);
    }

    private void accessRoute(int delayCount) {
        //显示splash screen
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //如果有用户登录文件
                if (myApp.logonUser.isLogon) {
                    //启动服务
                    Intent intent = new Intent(NavActivity.this, BackgroundService.class);
                    startService(intent);
                    Intent intent1 = new Intent(NavActivity.this, MusicPlayService.class);
                    startService(intent1);

                    //直接进入首页
                    startActivity( new Intent(NavActivity.this, ExhibitionActivity.class));
                    NavActivity.this.finish();
                } else {
                    //否则弹出登录界面
                    startActivity(new Intent(NavActivity.this, LoginActivity.class));
                    NavActivity.this.finish();
                }
            }
        }, delayCount * 1000);

    }
}
